package sample;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import static java.lang.Math.cos;
import static java.lang.Math.sin;

public class Controller {
    @FXML
    private Line line_hour;
    @FXML
    private Line line_minute;
    @FXML
    private Line line_second;
    @FXML
    private Text text_date;
    @FXML
    private Text text_time;
    @FXML
    private Button btn_start;
    @FXML
    private Button btn_close;

    private  boolean clockState = false;//时钟状态
    //当前的时间
    private Calendar dateTime;
    private int hours;
    private int minutes;
    private int seconds;

    //格式化日期、时间
    private static SimpleDateFormat dateDF = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat timeDF = new SimpleDateFormat("HH:mm:ss");
    //根据JavaFX Scene Builder中的组件,计算到的常量
    //三个指针的(起点X,起点Y)都相同,根据EndX就是其长度
    private static double centerX = 0;//指针的起点X
    private static double centerY = 0;//指针的起点Y
    private static double hourLength = 60;//时针的长度
    private static double minuteLength = 75;//分针的长度
    private static double secondLength = 110;//秒针的长度
    //引用的全局常量
    private static double p2i = 6.28;//p2i =PI * 2

    //开启时钟,start按钮的onAction事件
    public void startClock() {
        clockState = true;
        clock();
    }
    //关闭时钟,close按钮的onAction事件
    public void closeClock() {
        clockState = false;
        clock();
    }

    //更新获取当前的时间,设置到对象的字段中
    private void setTime() {
        this.dateTime = Calendar.getInstance();
        this.hours = this.dateTime.get(Calendar.HOUR);
        this.minutes = this.dateTime.get(Calendar.MINUTE);
        this.seconds = this.dateTime.get(Calendar.SECOND);
    }

    //更新Text文本框的日期时间
    private void setTextDateTime() {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                //更新JavaFX的主线程的代码放在此处
                text_time.setText("时间: " + timeDF.format(dateTime.getTime()));
                text_date.setText("日期: " + dateDF.format(dateTime.getTime()));
            }
        });

    }

    //显示start、close按钮交叉显示
    //即点击start按钮后,start按钮看不见,而close看得见
    // 点击close按钮后,close按钮看不见,而start看得见
    private void showButton() {
        btn_close.setVisible(clockState);
        btn_start.setVisible(!clockState);
    }

    //改变时钟指针的结束坐标(时钟指针移动算法)
    private void setClockPointer() {
        double hourX = centerX + hourLength * sin((hours + minutes / 60)* p2i / 12);
        double hourY= centerY - hourLength * cos((hours + minutes / 60) * p2i / 12);
        double minuteX = centerX + minuteLength * sin(minutes * p2i / 60);
        double minuteY = centerY - minuteLength * cos(minutes * p2i / 60);
        double secondX = centerX + secondLength * sin(seconds * p2i / 60);
        double secondY = centerY - secondLength * cos(seconds * p2i / 60);

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                //更新JavaFX的主线程的代码放在此处
                line_hour.setEndX(hourX);
                line_hour.setEndY(hourY);
                line_minute.setEndX(minuteX);
                line_minute.setEndY(minuteY);
                line_second.setEndX(secondX);
                line_second.setEndY(secondY);
            }
        });

    }

    //时钟状态为true就开启新线程,为false时不开启线程(或结束线程)[JDK8 or 9]
    private void clock() {
        showButton();
        if (clockState)
            new Thread(() -> {
                try {
                    while (clockState) {
                        setTime();//刷新当前时间
                        setTextDateTime();//刷新文本框内容
                        setClockPointer();//刷新指针的位置
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
    }

}
